<?php 
$weekarr = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$week = $weekarr[date("w")];
?>
<script type="text/javascript" SRC="<?=base_url()?>resource/js/jquery.nyroModal.pack.js"></script>
<link href="<?=base_url()?>resource/css/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?=base_url()?>resource/js/swfobject.js"></script>
<script type="text/javascript" src="<?=base_url()?>resource/js/jquery.uploadify.v2.1.0.min.js"></script>
<script>
var buildingStr = "";
var buildingsArray;
var progress;
var Administry = {}
Administry.progress = function (el, val, max) {
    var duration = 400;
    var span = $(el).find("span");
    var b = $(el).find("b");
    var w = Math.round((val / max) * 100);
    $(b).fadeOut('fast');
    $(span).animate({
        width: w + '%'
    }, duration, function () {
        $(el).attr("value", val);
        $(b).text(w + '%').fadeIn('fast');
    });
}

Administry.progress2 = function (el, val, max,rate) {
    var duration = 0;
    var span = $(el).find("span");
    var b = $(el).find("b");
    var w = (val*100 / max).toFixed(2);
    //$(b).fadeOut('fast');
    $(span).animate({
        width: w*rate + '%'
    }, duration, function () {
        $(el).attr("value", val);
        $(b).text(w + '%').fadeIn('fast');
    });
}

function writeObj(obj){ 
    var description = ""; 
    for(var i in obj){   
        var property=obj[i];   
        description+=i+" = "+property+"\n";  
    }   
    alert(description); 
} 
$(function(){
    $("#uploadify").uploadify({
        'uploader'       : '<?=base_url()?>resource/swf/uploadify.swf',
        'script'         : '<?=base_url()?>index/uploadCandidate',
        'cancelImg'      : '<?=base_url()?>resource/img/cancel.png',
        'queueID'        : 'fileQueue',
        'auto'           : true,
        'multi'          : false,
        'buttonText': '选择点位',
        'simUploadLimit' : 1,
        'queueSizeLimit' : 1,
        'fileExt': '*.xlsx;*.xls',
        'fileDesc': '请选择Excel(*.xlsx;*.xls)',
        onComplete: function(event, queueID, fileObj, response, data) {
            var responseArr = response.split(";");
            buildingsArray=new Array();
            for(var i = 0;i < responseArr.length;i++){
                var linestr = responseArr[i];
                var lineArr = linestr.split(",");
                buildingsArray[i] = lineArr;
            }
            if(buildingsArray.length > 1 && buildingsArray[1].length == 9){
                var htmlStr = "<table>";
                htmlStr += "<tr>";
                for(var i = 0;i < buildingsArray[0].length;i++){
                    htmlStr += "<th>"+buildingsArray[0][i]+"</th>";
                }
                htmlStr += "</tr>";
                for(var i = 1;i < buildingsArray.length;i++){
                    htmlStr += "<tr>";
                    for(var j = 0;j < buildingsArray[i].length;j++){
                        htmlStr += "<td>"+buildingsArray[i][j]+"</td>";
                    }
                    htmlStr += "</tr>";
                }
                htmlStr += "</table>";
                $("#buildingsDiv").html(htmlStr);
                $(".box").css("display","block");
                buildingStr = response;
            }else{
                $("#buildingsDiv").html("请参照上图示例上传excel");
            }
        },
        onSelect:function(){
            buildingStr = "";
        },
        onError: function(event, queueID, fileObj) {
            alert("文件:" + fileObj.name + "上传失败");
        }
    });
    
    function importBuildings(index){
        var postData = "";
        for(var j = 0;j < buildingsArray[index].length;j++){
            postData += buildingsArray[index][j];
            if(j+1<buildingsArray[index].length){
                postData += ",";
            }
        }
        $.ajax({
            url:'<?=base_url("index/saveCandidate")?>',
            type:'post',
            data:{
                'candidateStr':postData
            },
            success:function(msg){
                var sum = buildingsArray.length-1;
                var rate = ((index*10000)/sum)/100;
                
                var mydate = new Date();
                var t=mydate.toLocaleString();
                if(msg != 0){
                    $("#progress").after("<a style='color:green;'>"+t+"</a> "+msg);
                }else{
                    $("#progress").after("<a style='color:red;'>"+t+" "+buildingsArray[index][0]+"保存失败！</a><br/>"+msg);
                }
                Administry.progress2("#progress", rate, 100,3.6);
                //console.log(rate);
                if(index+1 < buildingsArray.length){
                    index++;
                    importBuildings(index);
                }else{
                    $("#progress").after("<a style='color:green;'>当前操作完成。</a><br/>");
                }
            }
        });
    }
    
    $("#save").click(function(){
        progress = $("#progress");
        if(buildingStr != ""){
            Administry.progress("#progress", 0, 100);
            $("#foobar").prepend("<a style='color:green;'>开始保存...</a><br/>");
            importBuildings(1);
        }else{
            $("#foobar").html("请上传点位信息");
        }
    });
    
    Administry.progress("#progress",0, 100);
    
    $('.nyroModal').nyroModal({
        endRemove: function () {
            window.location.reload();
        }
    });
});
</script>
<div id="candidate" class="indexcontentdiv">
    <div class="positiondiv">
        <table>
            <tr>
                <td class="td-left">
                    <img src="<?=base_url()?>resource/img/home_ico.png" width="21" height="17">
                    <?=$this->lang->line("Data Entry")?> > <?=$this->lang->line("Candidate")?>
                </td>
                <td class="td-right">
                    <img src="<?=base_url()?>resource/img/menu2.png" width="27" height="24"><?=$this->lang->line("TodayIS")?> <?=$week?>,<?=date('M d,Y')?>
                </td>
            </tr>
        </table>
    </div>
    <div class="detail-menu">
        <?php if($state==0){?>
            <div class="active1 detail-active menu" id="menu1"><?=$this->lang->line("Normal")?></div>
            <div class="active2 single-menu menu" id="menu2"><?=$this->lang->line("InProfile")?></div>
            <div class="active3 single-menu menu" id="menu3"><?=$this->lang->line("Archived")?></div>
            <div class="active4 single-menu menu" id="menu4"><?=$this->lang->line("Import")?></div>
        <?php }else if($state==1){?>
            <div class="active1 single-menu menu" id="menu1"><?=$this->lang->line("Normal")?></div>
            <div class="active2 detail-active menu" id="menu2"><?=$this->lang->line("InProfile")?></div>
            <div class="active3 single-menu menu" id="menu3"><?=$this->lang->line("Archived")?></div>
            <div class="active4 single-menu menu" id="menu4"><?=$this->lang->line("Import")?></div>
        <?php }else if($state==4){?>
            <div class="active1 single-menu menu" id="menu1"><?=$this->lang->line("Normal")?></div>
            <div class="active2 single-menu menu" id="menu2"><?=$this->lang->line("InProfile")?></div>
            <div class="active3 detail-active menu" id="menu3"><?=$this->lang->line("Archived")?></div>
            <div class="active4 single-menu menu" id="menu4"><?=$this->lang->line("Import")?></div>
        <?php }?>
        <div style="width:0px;"></div>
    </div>
    <div class="list-div" style="margin-top:0px;">
        <?php echo form_open('index/index'); ?>
        <table class="top-table">
            <tr>
                <td>
                    <input type="hidden" value="<?=$ordername?>" name="ordername" id="ordername" />
                    <input type="hidden" value="<?=$state?>" name="state" />
                </td>
                <td width="240" class="center"><input type="text" placeholder="<?=$this->lang->line("searchmsg")?>" 
                    name="condition" value="<?=$condition?>"/></td>
                <td width="90" class="center"><input type="submit" value="<?=$this->lang->line("Search")?>" /></td>
                <td width="110" class="center"><input type="button" value="<?=$this->lang->line("New")?>" id="new"/></td>
                <?php if($this->session->userdata('user')->role == 0){?>
                <td width="160" class="center"><a href="#" id="export2excel"  class="button"><?=$this->lang->line("Export2Excel")?></a></td>
                <?php }?>
            </tr>
        </table>
        <?php echo form_close(); ?>
        <table class="list-table">
            <tr>
                <th class="order" ordername="name"><?=$this->lang->line("Name")?><img src="<?=base_url()?>resource/img/arrow_no.png" /></th>
                <th width="70" class="order" ordername="gender"><?=$this->lang->line("Gender")?><img src="<?=base_url()?>resource/img/arrow_no.png" /></th>
                <th width="100" class="order" ordername="nationality"><?=$this->lang->line("Nationality")?><img src="<?=base_url()?>resource/img/arrow_no.png" /></th>
                <th width="80" class="order" ordername="location"><?=$this->lang->line("Location")?><img src="<?=base_url()?>resource/img/arrow_no.png" /></th>
                <th width="180" class="order" ordername="overbeensgp"><?=$this->lang->line("EverBeanSingapore")?><img src="<?=base_url()?>resource/img/arrow_no.png" /></th>
                <th width="100" class="order" ordername="position"><?=$this->lang->line("Position")?><img src="<?=base_url()?>resource/img/arrow_no.png" /></th>
                <th width="110" class="order" ordername="updatetime"><?=$this->lang->line("UpdateTime")?><img src="<?=base_url()?>resource/img/arrow_no.png" /></th>
                <th width="100" class="order" ordername="speciality"><?=$this->lang->line("Speciality")?><img src="<?=base_url()?>resource/img/arrow_no.png" /></th>
                <?php if($this->session->userdata('department') == 1){?>
                <th width="60"></th>
                <?php if($state==0){?>
                <th width="60"></th>
                <th width="60"></th>
                <?php }else if($state==1){?>
                <th width="60"></th>  
                <?php }else if($state==4){?>
                <th width="60"></th>    
                <?php }}?>
            </tr>
            <?php foreach ($candidates as $key => $value) {?>
            <tr>
                <td><?=$value['name']?></td>
                <td><?=$value['gender']==1?"M":"F"?></td>
                <td><?=$value['nationality']==1?"Malaysia":($value['nationality']==2?"China":($value['nationality']==3?"Vietnam":($value['nationality']==4?"Myanmar":$value['nationality'])))?></td>
                <td><?=$value['location']?></td>
                <td><?=$value['overbeensgp']==0?"Y":"N"?></td>
                <td><?=$value['position']?></td>
                <td><?=date('d-m-Y',$value['updatetime'])?></td>
                <td><?=$value['speciality']?></td>
                <?php if($this->session->userdata('department') == 1){?>
                <td><a href="javascript:void(0)" class="buttona detail" candidateid="<?=$value['id']?>"><img src="<?=base_url()?>resource/img/detail.png"/><?=$this->lang->line("Edit")?></a></td>
                <?php if($state==0){?>
                <td>
                    <a href="javascript:void(0)" class="buttona delete" candidateid="<?=$value['id']?>">
                        <img src="<?=base_url()?>resource/img/delete.png"/><?=$this->lang->line("Delete")?></a>
                </td>
                <td>
                    <a href="javascript:void(0)" class="buttona archived" profileid="<?=$value['id']?>">
                        <img src="<?=base_url()?>resource/img/archived.png"/><?=$this->lang->line("Archieved")?>
                    </a>
                </td>
                <?php }else if($state == 1){?>
                <td>
                    <a href="javascript:void(0)" class="buttona restore" profileid="<?=$value['id']?>">
                            <img src="<?=base_url()?>resource/img/restore.png"/><?=$this->lang->line("Restore")?>
                        </a>
                </td>
                <?php }else if($state == 4){?>
                <td>
                    <a href="javascript:void(0)" class="buttona restore" profileid="<?=$value['id']?>">
                            <img src="<?=base_url()?>resource/img/restore.png"/><?=$this->lang->line("Restore")?>
                        </a>
                </td>
                <?php }}?>
            </tr>
            <?php }
            if(count($candidates) == 0){?>
            <tr>
                <td colspan="8">
                    NO DATA
                </td>
            </tr>
            <?php }?>
            <tr>
                <td colspan="8" class="pagination">
                    <?php echo $page;?>
                </td>
            </tr>
        </table>
    </div>
    <div class="import-div" style="display:none;">
        <table class="top-table">
            <tr><td>请上传excel文件，格式如下</td></tr>
            <tr><td><img src="<?= base_url()?>resource/img/import_candidate_img.png" /></td></tr>
            <tr>
                <td>
                    ps:性别：M/F<br/>
                    &#8194;&#8194;&#8194;国籍：Malaysia/China/Vietnam/Myanmar/Other<br/>
                    &#8194;&#8194;&#8194;当前位置：singapore/oversea
                </td>
            </tr>
        </table>
        <table class="content-table">
            <tr>
                <td>
                    <input type="file" name="uploadify" id="uploadify"/>
                    <div id="fileQueue"></div>
                    <div class="box box-info" style="margin-top:10px;display:none;">
                        上传详细 &nbsp; &nbsp;
                        <a HREF="#foobar" class="nyroModal btn btn-green big" title="保存点位" id="save">保存</a>
                        <div id='foobar'>
                            <div id="progress" class="progress width3 progress-red"><span><b></b></span></div>
                        </div>
                    </div>
                    <style>
                        #buildingsDiv table{width:100%;}
                        #buildingsDiv table th{height:40px;text-align:center;}
                        #buildingsDiv table td{height:40px;text-align:center;}
                    </style>
                    <div id="buildingsDiv"></div>
                </td>
            </tr>
        </table>
    </div>
</div>
<script>
$(".menu").click(function(){
    var id = $(this).attr("id");
    var url = "<?=base_url()?>index/index";
    if(id == "menu1"){
        url += "?state=0";
    }else if(id == "menu2"){
        url += "?state=1";
    }else if(id == "menu3"){
        url += "?state=4";
    }
    if(id == "menu4"){
        $(".list-div").css("display","none");
        $(".import-div").css("display","block");
        $(".menu").removeClass("detail-active");
        $("#menu4").addClass("detail-active");
    }else{
        location.href = url;
    }
});
    
$(".detail").click(function(){
    var id = $(this).attr("candidateid");
    var url = '<?=base_url()?>index/candidate_detail/'+id;
    location.href=url;
});

$(".delete").click(function(){
    if(confirm("Are you sure delete this candidate?")){
        var id = $(this).attr("candidateid");
        var url = '<?=base_url()?>index/deletecandidate?id='+id;
        location.href=url;
    }
});

$("#new").click(function(){
    var url = '<?=base_url()?>index/new_candidate';
    location.href=url;
});

var url = '<?=base_url()?>index/index';
var order = "<?=$ordername?>";
var orders = order.split(" ");
$("th").each(function(){
    var ordername = $(this).attr("ordername");
    if(orders[0] == ordername){
        $(this).children("img").attr("src","<?=base_url()?>resource/img/arrow_"+orders[1]+".png");
    }
});

$(".archived").click(function(){
    var profileid = $(this).attr("profileid");
    location.href = "<?=base_url()?>profile/archived?id="+profileid+"&type=1";
});

$(".restore").click(function(){
    var profileid = $(this).attr("profileid");
    location.href = "<?=base_url()?>profile/restore?id="+profileid+"&type=1";
});
$("#export2excel").click(function(){
    
    var url = "<?=base_url()?>index/export2excel?";
    var data = $("#myform").serialize();
    location.href = url+data;
});
</script>
